package com.newhero.app.report.utils;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.io.Writer;

import java.util.HashMap;

import java.util.Map;

import javax.servlet.ServletContext;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import freemarker.template.Configuration;

import freemarker.template.DefaultObjectWrapper;

import freemarker.template.Template;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class exportWord {

	// word模板

	public static final String word_template = "WEB-INF/ftl/report/template/test.ftl";

	// 存放模板的目录

	public static final String template_path = "WEB-INF/ftl/report/template";

	// 临时文件

	public static final String word_temp = "WEB-INF/ftl/report/template/temp.doc";

	HttpServletRequest request;
	HttpServletResponse response;
	
	public String executeExport() {
		

		try {

			Map dataMap = new HashMap();

			if (getData(request, response, dataMap)) {

				File previewFile = new File(request.getSession()
						.getServletContext().getRealPath(word_temp));

				InputStream is = new FileInputStream(previewFile);

				response.reset();

				response.setContentType("application/vnd.ms-word;charset=UTF-8");

				response.setHeader("Content-Disposition",
						"attachment; filename=export.doc");

				OutputStream os = response.getOutputStream();

				byte[] b = new byte[1024];

				int len;

				while ((len = is.read(b)) != -1) {

					os.write(b, 0, len);

				}

				is.close();

				os.flush();

				os.close();

			}

		} catch (Exception e) {

			e.printStackTrace();

		}

		return null;

	}

	public static Template configTemplate(HttpServletRequest request,
			String temp) {

		Template template = null;

		try {

			Configuration cfg = new Configuration();

			ServletContext application = request.getSession()
					.getServletContext();

			System.out.println(application.getRealPath(template_path));

			cfg.setDirectoryForTemplateLoading(new java.io.File(application
					.getRealPath(template_path)));

			cfg.setObjectWrapper(new DefaultObjectWrapper());

			template = cfg.getTemplate(temp, "utf-8");

		} catch (Exception e) {

			e.printStackTrace();

		}

		return template;

	}

	public static void toPreview(HttpServletRequest request, String temp,
			Map<?, ?> root) {

		try {

			String previewPath = request.getSession().getServletContext()
					.getRealPath("")
					+ word_temp;

			Template tmplate = configTemplate(request, temp);

			FileOutputStream fis = new FileOutputStream(previewPath);

			Writer out = new OutputStreamWriter(fis, "utf-8");

			tmplate.process(root, out);

			out.flush();
			out.close();

		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	public boolean getData(HttpServletRequest request,
			HttpServletResponse response, Map dataMap) {
		dataMap.put("test", "测试");
		dataMap.put("guojia", "中国");
		dataMap.put("shoudu", "首都");
		toPreview(request, word_template, dataMap);
		return true;
	}


}
